/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSVisualScene.cpp
 *
 *  Created on: 2010/03/04
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSVisualScene.h"
#include "RSNodeBase.h"

namespace Realsphere {

    RSVisualScene::RSVisualScene ( const QString& rId, const QString& rName ) :
        RSUIDElement ( rId, RS_VISUAL_SCENE_TAG )
    {
        SetName ( rName, false );
    }

    RSVisualScene::~RSVisualScene ()
    {
        mNodes.clear ();
    }

    RSNodeBase* RSVisualScene::GetNode ( const QString& rId ) const
    {
        RSNodesIterator iterator = RSNodesIterator ( mNodes );

        while ( iterator.hasNext () )
        {
            RSNodeBase* node_base = iterator.next ();
            QString node_base_id = node_base->Id ();

            if ( node_base_id == rId )
                return node_base;
        }

        return NULL;
    }

    RSNodeBase* RSVisualScene::GetNode ( unsigned int rIndex ) const
    {
        return mNodes[rIndex];
    }

    int RSVisualScene::IndexOf ( RSNodeBase* rNodeBase )
    {
        return mNodes.indexOf ( rNodeBase );
    }

    unsigned int RSVisualScene::NodesSize ( bool recursive )
    {
        unsigned int size = mNodes.size ();

        if ( !recursive )
            return size;

        RSNodesIterator iterator = RSNodesIterator ( mNodes );

        while ( iterator.hasNext () )
        {

            RSNodeBase* node_base = iterator.next ();
            size += node_base->ChildNodesSize ( true );
        }

        return size;
    }

    RSNodes RSVisualScene::Nodes () const
    {
        return mNodes;
    }

    RSNodesIterator RSVisualScene::NodesIterator () const
    {
        return RSNodesIterator ( mNodes );
    }

    bool RSVisualScene::IsRootElement () const
    {
        return false;
    }

    RSError RSVisualScene::AddNode ( RSNodeBase* rNodeBase )
    {
        RSError error = AddChildElement ( rNodeBase );
        return error;
    }

    RSError RSVisualScene::RemoveNode ( RSNodeBase* rNodeBase )
    {
        RSError error = RemoveChildElement ( rNodeBase );
        return error;
    }

    RSError RSVisualScene::RemoveNode ( const QString& rId )
    {
        RSNodeBase* node_base = GetNode ( rId );
        RSError error = RemoveNode ( node_base );
        return error;
    }

    RSError RSVisualScene::RemoveNode ( unsigned int rIndex )
    {
        RSNodeBase* node_base = GetNode ( rIndex );
        RSError error = RemoveNode ( node_base );
        return error;
    }

    RSError RSVisualScene::BeforeAddChildElement ( RSElement* rChild )
    {

        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {

            if ( rChild->InstanceOf ( RS_NODE_BASE_CLASS ) )
            {
                error = RS_NO_ERROR;
            }
        }

        return error;
    }

    bool RSVisualScene::AfterAddChildElement ( RSElement* rChild )
    {

        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled )
        {

            if ( rChild->InstanceOf ( RS_NODE_BASE_CLASS ) )
            {

                RSNodeBase* node_base = RSNodeBaseCast ( rChild );
                mNodes.append ( node_base );
                handled = true;

                emit NodeAdded ( node_base );
            }
        }

        return handled;
    }

    bool RSVisualScene::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled )
        {

            if ( rChild->InstanceOf ( RS_NODE_BASE_CLASS ) )
            {

                RSNodeBase* node_base = RSNodeBaseCast ( rChild );
                int index = mNodes.indexOf ( node_base );
                mNodes.removeAt ( index );
                handled = true;

                emit NodeRemoved ( node_base );
            }
        }

        return handled;
    }
}
